<template>
  <div class="comment-container">
    <div class="comment-top">
      <div class="comment-search">
        <el-select v-model="workId" filterable placeholder="请输入作品名称" @change="changeWork">
          <el-option v-for="(item, i) in workList" :key="i" :label="item.name" :value="item.data_id"></el-option>
        </el-select>
      </div>
      <div class="comment-btn">
        <el-button type="primary" size="small" @click="isShowDialog = true">新增</el-button>
      </div>
    </div>
    <el-scrollbar style="height: calc(100vh - 160px)" class="scrollbar" id="scroll-bar">
      <el-table :data="list" row-key="data_id" :tree-props="{ children: 'children' }">
        <el-table-column label="评论内容" prop="comment"></el-table-column>
        <el-table-column label="评论用户" prop="user_name"></el-table-column>
        <el-table-column label="最后修改时间" width="160" prop="data_update_time"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" @click="replyComment(scope.row.data_id)" size="mini">回复评论</el-button>
            <el-button type="primary" @click="updBanner(scope.row.data_id, scope.row.comment)" size="mini">
              修改
            </el-button>
            <el-button type="danger" @click="delBanner(scope.row.data_id)" size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <paging ref="page" :func="getList"></paging>
    </el-scrollbar>
    <el-dialog @closed="onClose" title="提示" :visible.sync="isShowDialog">
      <el-form :model="params" :rules="rules" ref="dataParams" label-width="120px" class="demo-dataParams">
        <el-form-item label="评论" prop="comment_name">
          <el-input v-model="params.comment_name" type="textarea" placeholder="请输入评论"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="isShowDialog = false">取 消</el-button>
        <el-button type="primary" @click="dialogConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import paging from '@/components/paging';
  import commentManage from '@/api/comment';
  import { getImageUrl } from '@/utils/image';
  import artManage from '@/api/works';

  export default {
    name: 'comment-view',
    components: {
      paging,
    },
    data() {
      return {
        list: [],
        isShowDialog: false,
        params: {
          comment_name: '',
        },
        comment_id: '',
        rules: {
          comment_name: [{ required: true, message: '请输入评论', trigger: 'blur' }],
        },
        workId: '',
        workList: [],
        parentWorkId: '',
      };
    },
    mounted() {
      this.getList();
      artManage.getArt({ notPagination: true }).then((res) => {
        this.workList = res.item_list;
      });
    },
    methods: {
      getImageUrl,
      async getList(callback) {
        let res = await commentManage.getComment({ works_id: this.workId });

        let arr = res.item_list.sort((a, b) => a.parent_id - b.parent_id);

        var obj = {};
        var newArr = [];
        for (let i = 0; i < arr.length; i++) {
          let m = arr[i];
          obj[m.data_id] = {
            ...m,
            children: [],
          };
          if (obj[m.parent_id]) obj[m.parent_id].children.push(obj[m.data_id]);
          else newArr.push(obj[m.data_id]);
        }

        this.list = newArr;
        callback && callback(this.list, res.total_count);
      },
      replyComment(id) {
        this.params = { comment_name: '' };
        this.isShowDialog = true;
        this.parentWorkId = id;
      },
      updBanner(data_id, comment_name) {
        this.updId = data_id;
        this.params.comment_name = comment_name;
        this.isShowDialog = true;
      },  
      onClose() {
        this.updId = '';
        this.parentWorkId = '';
        this.dataParams = {
          name: '',
          comment: '',
          art_id: '',
          type: '', //类型
          art_name: '',
        };
      },
      async delBanner(data_id) {
        this.$alert('是否确定删除评论', '删除', {
          confirmButtonText: '确定',
          callback: async (action) => {
            if (action === 'confirm') {
              await commentManage.delComment(data_id);
              this.getList();
            }
          },
        });
      },
      async dialogConfirm() {
        if (!this.params.comment_name || !this.workId) {
          this.$message({ message: '请选择作品或评论不能为空！', type: 'warning' });
          return;
        }
        let res = null;
        if (this.updId) {
          res = await commentManage.updComment(this.updId, { comment: this.params.comment_name });
        } else {
          let params = { works_id: this.workId, comment: this.params.comment_name };

          if (this.parentWorkId) params.parent_id = this.parentWorkId;
          res = await commentManage.setComment(params);
        }
        if (res) {
          this.isShowDialog = false;
          this.getList();
        }
      },
      async changeWork(v) {
        await this.getList();
      },
    },
  };
</script>

<style lang="less" scoped>
  .comment-container {
    height: 100%;
    .comment-top {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 12px;
      .comment-search {
        display: flex;
      }
    }
    .comment-list {
      display: flex;
      flex-flow: row wrap;
      width: 100%;
      height: 100%;
      .comment-item {
        width: calc(50% - 6px);
        height: 100px;
        display: flex;
        margin-bottom: 6px;
        border: 1px solid #f3f3f3;
        .item-content {
          margin: 10px;
          p {
            line-height: 40px;
          }
        }
      }
    }
  }
</style>
